package cn.xiaoyu.su.manager.impl;

import cn.xiaoyu.su.enums.PluginLevelEnum;
import cn.xiaoyu.su.enums.PluginTypeEnum;
import cn.xiaoyu.su.manager.TrafficManager;
import cn.xiaoyu.su.mapper.TrafficMapper;
import cn.xiaoyu.su.model.TrafficDO;
import cn.xiaoyu.su.util.TimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class TrafficManagerImpl implements TrafficManager {

    @Autowired
    private TrafficMapper trafficMapper;

    /**
     * 新增流量包
     * @param trafficDO
     * @return
     */
    @Override
    public int add(TrafficDO trafficDO) {
        return trafficMapper.insert(trafficDO);
    }

    /**
     * 分页查询可用的流量包
     * @param page
     * @param size
     * @param accountNO
     * @return
     */
    @Override
    public IPage<TrafficDO> pageAvailable(int page, int size, Long accountNO) {

        Page<TrafficDO> pageInfo = new Page<>(page, size);
        String today = TimeUtil.format(new Date(),"yyyy-MM-dd");

        return trafficMapper.selectPage(pageInfo,new QueryWrapper<TrafficDO>().eq("account_no",accountNO)
                .ge("expired_date",today).orderByDesc("gmt_create"));

    }

    /**
     * 查询详情
     * @param trafficId
     * @param accountNo
     * @return
     */
    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, Long accountNo) {
        return trafficMapper.selectOne(new QueryWrapper<TrafficDO>().eq("id",trafficId)
                .eq("account_no",accountNo));
    }


    /**
     * 删除过期流量包
     * @return
     */
    @Override
    public boolean deleteExpiredTraffic() {

        int rows = trafficMapper.delete(new QueryWrapper<TrafficDO>().le("expired_date",new Date()));
        log.info("删除过期流量包行数：rows={}",rows);
        return true;
    }


    /**
     * 增加某个流量包天使用次数
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int addDayUsedTimes(Long accountNo, Long trafficId, int usedTimes) {

        return trafficMapper.addDayUsedTimes(accountNo,trafficId,usedTimes);
    }

    /**
     * 查找未过期流量包列表,不一定可用，可能超过次数了
     * @param accountNo
     * @return
     */
    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(new Date(), "yyyy-MM-dd");

        List<TrafficDO> trafficDOList = trafficMapper.selectList(new QueryWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .eq("plugin_type", PluginTypeEnum.SHORT_LINK)
                .ge("expired_date", today));
        return trafficDOList;
    }

    /**
     * 恢复某个流量包的天使用次数，回滚流量包
     * @param accountNo
     * @param trafficId
     * @param usedTimes
     * @return
     */
    @Override
    public int releaseUsedTimes(Long accountNo, Long trafficId, Integer usedTimes) {
        return trafficMapper.releaseUsedTimes(accountNo,trafficId,usedTimes);
    }

    /**
     * 批量更新流量包使用次数为0
     * @param accountNo
     * @param unUpdatedTrafficIds
     * @return
     */
    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {

        int rows = trafficMapper.update(null, new UpdateWrapper<TrafficDO>()
                .eq("account_no", accountNo)
                .in("id", unUpdatedTrafficIds)
                .set("day_used", 0));
        return rows;
    }
}
